package com.cloud.logger.zchain.zchainserver.service;

import com.alibaba.fastjson.JSONObject;
import com.cloud.logger.zchain.zchainserver.base.PagePo;
import com.cloud.logger.zchain.zchainserver.entity.AngelaLogger;
import com.cloud.logger.zchain.zchainserver.entity.vo.AngelaLoggerVo;
import com.cloud.logger.zchain.zchainserver.entity.po.AngelaLoggerPo;
import com.google.common.base.Strings;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/11/7
 * Time: 14:13
 */
@Service
public class AngelaEntityService {

    @Resource
    private MongoTemplate mongoTemplate;

    public List<AngelaLoggerVo> findLoggerByPage(PagePo<AngelaLoggerPo> pagePo){

        Criteria criteria = new Criteria();
        criteria.where("1").is("1");
//        criteria.and("pattern").is("INTERCEPTOR");
        AngelaLoggerPo angelaLoggerPo = pagePo.getFields();

        if(angelaLoggerPo==null){
            List<AngelaLoggerVo> entityList = mongoTemplate.find(new Query(),AngelaLoggerVo.class);
            return entityList;
        }

        if(!StringUtils.isEmpty(angelaLoggerPo.getInterfaceName())){
            JSONObject elapsedTime = new JSONObject();
            criteria.and("interfaceName").regex(angelaLoggerPo.getInterfaceName());
        }

        if(angelaLoggerPo.getElapsedTime() != null){
            JSONObject elapsedTime = new JSONObject();
            criteria.and("elapsedTime").gte(angelaLoggerPo.getElapsedTime());
        }

        if(!Strings.isNullOrEmpty(angelaLoggerPo.getStartTime())){
            if(!Strings.isNullOrEmpty(angelaLoggerPo.getEndTime())){
                criteria.and("createTime").gte(angelaLoggerPo.getStartTime()).lt(angelaLoggerPo.getEndTime());
            }else {
                criteria.and("createTime").gte(angelaLoggerPo.getStartTime());
            }
        }else if(!Strings.isNullOrEmpty(angelaLoggerPo.getEndTime())){
            criteria.and("createTime").lte(angelaLoggerPo.getEndTime());
        }

        if(!Strings.isNullOrEmpty(angelaLoggerPo.getStatus())){
            criteria.and("status").is(angelaLoggerPo.getStatus());
        }

        if(!Strings.isNullOrEmpty(angelaLoggerPo.getInterfaceIp())){
            criteria.and("interfaceIp").is(angelaLoggerPo.getInterfaceIp());
        }

        if(!Strings.isNullOrEmpty(angelaLoggerPo.getScenic())){
            criteria.and("scenic").is(angelaLoggerPo.getScenic());
        }

        if(!Strings.isNullOrEmpty(angelaLoggerPo.getProject())){
            criteria.and("project").is(angelaLoggerPo.getProject());
        }

        Pageable pageable = new PageRequest(pagePo.getPageNo(), pagePo.getPageSize());

        List<AngelaLoggerVo> entityList = mongoTemplate.find(new Query(criteria).with(new Sort(Sort.Direction.DESC, "createTime")).with(pageable),AngelaLoggerVo.class);
        return entityList;
    }

    public List<AngelaLogger> findLoggerByUuid(String uuid) {
        Criteria criteria = new Criteria();
        criteria.where("1").is(1);
        criteria.and("parentNode").is(uuid);
        List<AngelaLogger> entityList = mongoTemplate.find(new Query(criteria).with(new Sort(Sort.Direction.DESC, "create_time")),AngelaLogger.class);
        for(AngelaLogger angelaLogger: entityList){
            angelaLogger.setChildren(findLoggerByUuid(angelaLogger.getUuid()));
        }
        return entityList;
    }
}
